package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.StringUtils;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.di.component.DaggerPhoneVerifyComponent;
import com.eduhzy.ttw.teacher.mvp.contract.PhoneVerifyContract;
import com.eduhzy.ttw.teacher.mvp.presenter.PhoneVerifyPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.xw.repo.XEditText;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;

@Route(path = RouterHub.APP_PHONEVERIFYACTIVITY)
public class PhoneVerifyActivity extends BaseActivity<PhoneVerifyPresenter> implements PhoneVerifyContract.View {
    @Autowired(name = Constants.TEMPLATE)
    String type;
    @Autowired(name = Constants.USERNAME)
    String user;
    @BindView(R.id.phone_verify_user)
    TextView phoneVerifyUser;
    @BindView(R.id.phone_verify_code)
    XEditText phoneVerifyCode;
    @BindView(R.id.phone_verify_send)
    Button phoneVerifySend;
    private MyCountDownTimer myCountDownTimer;

    private String messageCode;

    public String getMessageCode() {
        return messageCode;
    }

    @Override
    public void setMessageCode(String messageCode) {
        this.messageCode = messageCode;
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void cancelCounter() {
        myCountDownTimer.cancel();
        phoneVerifySend.setText("重发");
        //设置可点击
        phoneVerifySend.setEnabled(true);
        phoneVerifySend.setClickable(true);
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerPhoneVerifyComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_phone_verify; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        myCountDownTimer = new MyCountDownTimer(60000, 1000);
        getCode();


    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    private void getCode() {
        String text = String.format(getString(R.string.phone_verify_tip), Long.valueOf(user));
        phoneVerifyUser.setText(text);
        myCountDownTimer.cancel();
        myCountDownTimer.start();
        mPresenter.getSmsCode(user, type);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @OnClick({R.id.phone_verify_send, R.id.phone_verify_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.phone_verify_send:
                getCode();
                break;
            case R.id.phone_verify_next:
                KeyboardUtils.hideSoftInput(this);
                String code = phoneVerifyCode.getText().toString();
                if (StringUtils.isTrimEmpty(code)) {
                    RxUtil.showTipDialog(this, QMUITipDialog.Builder.ICON_TYPE_FAIL, "请输入短信验证码！", 1500);
                    return;
                }
                Message msg = Message.obtain();
                msg.what = EventBusTags.GET_CODE_SUCCESS;
                switch (type) {
                    case Constants.TYPE_REAL_NAME_AUTH:
                        msg.obj = code;
                        EventBus.getDefault().post(msg, RouterHub.APP_UPDATEPHONEACTIVITY);
                        break;
                    case Constants.TYPE_FIND_PASSWORD:
                        ARouter.getInstance().build(RouterHub.APP_RESETPASSWORDACTIVITY)
                                .withString(Intent.EXTRA_TITLE, Constants.RESET_PASSWORD)
                                .withString(Constants.USERNAME, user)
                                .withString(Constants.MESSAGECODE, code)
                                .navigation();

                        break;
                    case Constants.TYPE_REGISTER:
                        msg.obj = code;
                        EventBus.getDefault().post(msg, RouterHub.APP_REGISTERACTIVITY);
                        break;
                    case "5":
                        msg.obj = code;
                        EventBus.getDefault().post(msg, RouterHub.APP_REGISTERACTIVITY);
                        break;
                    default:
                }

                break;
            default:
        }
    }

    @Subscriber(tag = EventBusTags.LOGIN)
    public void onEvents(Message msg) {
        switch (msg.what) {
            case EventBusTags.LOGIN_SUCCESS:
                killMyself();
                break;
        }
    }

    //复写倒计时
    private class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        //计时过程
        @Override
        public void onTick(long l) {
            //防止计时过程中重复点击
            phoneVerifySend.setClickable(false);
            phoneVerifySend.setEnabled(false);
            phoneVerifySend.setText(l / 1000 + "s");
        }

        //计时完毕的方法
        @Override
        public void onFinish() {
            //重新给Button设置文字
            phoneVerifySend.setText("重发");
            //设置可点击
            phoneVerifySend.setEnabled(true);
            phoneVerifySend.setClickable(true);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myCountDownTimer.cancel();
    }

}
